﻿#include "zz.h"

#if defined(__QG_H__)

//#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")

using namespace tpl;

int main(void)
{
	k_setlv(KLV_ALL);
	k_mesg("test \n");

	qgRdh* rdh = qgRdh::New("", 640, 480, QGSDF_LAYOUT, "test");

	if (rdh)
	{
		qgStub* stub = rdh->GetStub();

		qgFreeType* font = qgFreeType::New();

		qgTexture* tex = rdh->CreateTexture("haha", 64, 64, 0, 0, 0, QGCF_NONE);
		qgTexture* img = rdh->CreateTexture("e:\\z.png", 0);

		qgFpsCamera* cam = qgFpsCamera::New();
		cam->SetProcessLayout(true);
		cam->SetMoveSpeed(50.0f);
		cam->SetLocation(Vec3(0.0f, 50.0f, -300.0f));

		static qbLayoutElement s_le[] =
		{
			{QGDLOU_POSITION, 0, QGDLOT_FLOAT3, 0, 0, },
			{QGDLOU_TEXTURE, 0, QGDLOT_FLOAT2, 0, 0, },
			{QGDLOU_NORMAL, 0, QGDLOT_FLOAT3, 0, 0, },
			{QGDLOU_TANGENT, 0, QGDLOT_FLOAT3, 0, 0, },
			{QGDLOU_BINORMAL, 0, QGDLOT_FLOAT3, 0, 0, },
		};
		qgVertexLayout* lo = rdh->CreateVertexLayout(K_COUNTOF(s_le), s_le);

		qgShader* shd = rdh->CreateShader(NULL);
		if (!shd->BindFile(QGSHADER_VS, "e:\\zz.hlsl", 0))
			k_breakpoint();
		if (!shd->BindFile(QGSHADER_PS, "e:\\zz.hlsl", 0))
			k_breakpoint();
		shd->Link();

		qgSmm* smm = qgSmm::New(0, "e:\\zz.smm", 0);
		for (int i = 0; i < smm->GetMeshCount(); i++)
		{
			qgSmmMeshRef m = smm->GetMeshNth(i);
			m->SetShader(shd, lo);
		}

		qgSmm* sxx = qgSmm::New(0, "e:\\xx.smm", 0);
		for (int i = 0; i < sxx->GetMeshCount(); i++)
		{
			qgSmmMeshRef m = sxx->GetMeshNth(i);
			m->SetShader(shd, lo);
		}

		float rot = 0.0f;

		while (rdh->Run())
		{
			float adv = stub->GetAdvance();

			qgRaszStateRef rs = qbStaticRaszState<QGFILL_SOLID, QGCULL_FRONT>::GetPtr();
			qgBlndStateRef bs = qbStaticBlndState<QGMASK_RGBA, QGBLO_ADD, QGBLF_SA, QGBLF_DA_INV>::GetPtr();
			qgSplrStateRef ss = qbStaticSplrState<QGFLTX_LINEAR>::GetPtr();
			qgDhslStateRef ds = qbStaticDhslState<false, QGCOP_LE>::GetPtr();

			if (rdh->Enter())
			{
				//
				cam->Update(adv);
				rdh->SetTM(QGTM_PROJ, cam->GetProjTM());
				rdh->SetTM(QGTM_VIEW, cam->GetViewTM());

				// 모델
				rdh->SetPropVec4(0, Vec4(*(Vec3*)&cam->GetViewTM()._41, 1.0f));	// 카메라
				rdh->SetPropVec4(1, Vec4(0.0f, 500.0f, -500.0f, 1.0f));	// 라이트
				rot = k_cprad(rot + KM_PI_HALF * adv);

				smm->SetRotation(Quat::RotationY(rot));
				smm->SetLocation(Vec3(100.0f, 0.0f, 0.0f));
				smm->Update(adv);
				smm->Draw();

				sxx->SetRotation(Quat::RotationY(rot));
				sxx->SetLocation(Vec3(-100.0f, 0.0f, 0.0f));
				sxx->Update(adv);
				sxx->Draw();

				// 스프라이트
				rdh->DrawSprite(Rect(10, 10, 110, 110), ColorValues::Blue);

				// 스프라이트 + 그림
				rdh->DrawSprite(Rect(120, 10, 220, 110), ColorValues::White, img);

				// 선
				Quad bound(150, 150, (float)stub->GetSize().x - 150, (float)stub->GetSize().y - 150);
				Vec3 vx[4] =
				{
					Vec3(bound.left, bound.top, 0.0f),
					Vec3(bound.right, bound.top, 0.0f),
					Vec3(bound.right, bound.bottom, 0.0f),
					Vec3(bound.left, bound.bottom, 0.0f),
				};
				ColorValue vc[] =
				{
					ColorValue(0xFFFF0000),
					ColorValue(0xFF00FF00),
					ColorValue(0xFF0000FF),
					ColorValue(0xFFFFFFFF),
				};
				rdh->DrawLines(true, 4, vx, vc);

				//
				font->WriteFormat(5, 5, 0, "TEST \xEC\x9E\x98\xEB\x90\x98\xEB\x9D\xBC! %.2f", stub->GetRunFps());

				//
				rdh->Leave();
				rdh->Flush();
			}
		}

		font->Unload();

		rdh->Unload();
	}

	return 0;
}

#endif
